using System;
using System.Data;
using System.Web.UI.WebControls;
using NCS.Library.Components.Security;
using NCS.Library.Components.Users;
using NCS.Library;
using NCS.Library.Components.Portal;
using NCS.Library.Utilities;
using NCS.Module.CMS.Library.CMSPermisions;
using NCS.Module.CMS.Library.Zones;

namespace NCS.Module.CMS.Admin
{
	public partial class UserPermision : AuthenticatedControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			lblUpdateStatus.Text = string.Empty;
			if (!IsPostBack)
			{
                DataTable dtUsers;
                if (!CurrentUser.IsAdmin && !CurrentUser.IsSuperAdmin)
                {
                    dtUsers = UserController.GetUsersByUser("", "", "", CurrentUser.UserID,ConvertUtility.ToInt32(AppEnv.PortalId()));
                }
                else
                {
                    dtUsers = UserController.GetUsers("", "", "", ConvertUtility.ToInt32(AppEnv.PortalId()), 1);
                }

                dtgUsers.DataSource = dtUsers;
				dtgUsers.RowDataBound += new GridViewRowEventHandler(dtgUsers_RowDataBound);
				dtgUsers.DataBind();
				
                //Fill portals
                dropPortal.Items.Clear();
                if (!CurrentUser.IsSuperAdmin && !CurrentUser.IsAdmin)
                {
                    PortalController.LoadPortalsForUserID(dropPortal.Items, CurrentUser.UserID);
                }
                else
                {
                    PortalController.LoadPortals(dropPortal.Items);
                }
                dropPortal.SelectedValue = AppEnv.PortalId();
			}
		}

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            LoadZones();
            CMSPermisionInfo curPermision = CMSPermisionController.GetPermision(ConvertUtility.ToInt32(txtUserID.Text), ConvertUtility.ToInt32(dropPortal.SelectedValue));
            rdoPermisions.SelectedIndex = -1;
            if (curPermision == null) return;

            MiscUtility.SelectItemFromList(rdoPermisions, curPermision.Permision.ToString());

            foreach (ListItem item in lstZones.Items)
                if (curPermision.Zones.IndexOf("|" + item.Value + "|") > -1) item.Selected = true;
                else item.Selected = false;
                       
        }

        protected void LoadZones()
        {
            lstZones.Items.Clear();
            ZoneController.LoadZones(lstZones.Items, dropPortal.SelectedValue, 0);
            lstZones.Items.Insert(0, new ListItem("Root", "0"));
        }

		protected void dtgUsers_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if(e.Row.RowType == DataControlRowType.DataRow)
			{
				DataRowView curdata = (DataRowView) e.Row.DataItem;

                if (e.Row.Cells[1].Text == AppEnv.ADMIN_EMAIL)
                {
                    if (CurrentUser.Email != AppEnv.ADMIN_EMAIL)
                    {
                        e.Row.Visible = false;
                    }
                    else
                    {
                        e.Row.Visible = true;
                    }
                }
			}
		}
		protected void dtgUsers_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			if (e.CommandName == "roles")
			{
				int userId = ConvertUtility.ToInt32(e.CommandArgument);
				UserInfo user = UserController.GetUser(userId);
				if(user == null) return;
				txtUserID.Text = user.UserID.ToString();
				lblUsername.Text = user.Username;

			}
		}
		protected void cmdUpdate_Click(object sender, EventArgs e)
		{
			if (lblUsername.Text == string.Empty || txtUserID.Text == string.Empty) return;
			try
			{
				int userId = ConvertUtility.ToInt32(txtUserID.Text);
				int permision = ConvertUtility.ToInt32(rdoPermisions.SelectedValue, -1);
				if(permision == -1)
				{
                    CMSPermisionController.RemoveUserPermision(userId, ConvertUtility.ToInt32(dropPortal.SelectedValue));
					 return;
				}
				string permisionZoneCollection = "|";
				foreach (ListItem item in lstZones.Items)
				{
					if (item.Selected) permisionZoneCollection += item.Value + "|";
				}
				if (permisionZoneCollection.Length == 1) permisionZoneCollection = string.Empty;
                CMSPermisionController.UpdateUserPermision(userId, permision, permisionZoneCollection, ConvertUtility.ToInt32(dropPortal.SelectedValue));
				lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
			}
			catch (Exception ex)
			{
				lblUpdateStatus.Text = ex.Message;
			}
		}
		protected void cmdCancel_Click(object sender, EventArgs e)
		{
			rdoPermisions.SelectedIndex = -1;
			lstZones.SelectedIndex = -1;
		}

        protected void dropPortal_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }

        protected void dtgUsers_PageIndexChanged(object sender, EventArgs e)
        {

        }

        protected void dtgUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            DataTable dtUsers;
            if (!CurrentUser.IsAdmin && !CurrentUser.IsSuperAdmin)
            {
                dtUsers = UserController.GetUsersByUser("", "", "", CurrentUser.UserID, ConvertUtility.ToInt32(AppEnv.PortalId()));
            }
            else
            {
                dtUsers = UserController.GetUsers("", "", "", ConvertUtility.ToInt32(AppEnv.PortalId()));
            }

            dtgUsers.DataSource = dtUsers;
            dtgUsers.PageIndex = e.NewPageIndex;
            dtgUsers.RowDataBound += new GridViewRowEventHandler(dtgUsers_RowDataBound);
            dtgUsers.DataBind();

        }
	}
}